package main

import (
	"os"
	"fmt"
	"bufio"
	"io"
	"strings"
)

func readFile(path string) {
	file, err := os.Open(path)
	if err != nil {
		fmt.Printf("read file error\n")
		return
	}
	defer file.Close()

	inputReader := bufio.NewReader(file)
	for {
		line, err := inputReader.ReadString('\n')
		if err == io.EOF || err != nil {
			fmt.Println("read eof or err")
			break
		}

		stringSlice := strings.Fields(line)

		if stringSlice[2] == "112.125.89.4" {
			file, err := os.OpenFile("/home/simon/output.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0755)
			if err != nil {
				fmt.Println("open file err:", err)
				return
			}
			var str = stringSlice[13:24]
			var agent string
			for _, v := range str {
				agent += v
			}
			agent += "\n"
			fmt.Printf("%s", agent)

			_, err = file.WriteString(agent)
			if err != nil {
				fmt.Println(err)
			}
		}
	}
}

func main() {
	readFile("/home/simon/merge.log")
}
